package org.dtrd.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.common.PathUtil;
import org.dtrd.config.system.Constant;
import org.dtrd.config.system.Constant.WechatMenuType;
import org.dtrd.config.wechat.api.impl.WechatApi;
import org.dtrd.config.wechat.config.WechatConstant;
import org.dtrd.config.wechat.config.WechatUtil;
import org.dtrd.config.wechat.entity.bean.officialaccount.WechatMenu;
import org.dtrd.config.wechat.entity.bean.officialaccount.WechatMenuButton;
import org.dtrd.config.wechat.entity.response.WechatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author qr
 * @date 2022/3/21 16:48
 */
@Api(tags = "微信菜单")
@RestController
@RequestMapping("/sys/wechat/menu")
@Slf4j
public class WechatMenuController {

    @Autowired
    private WechatApi wechatApi;

    @ApiOperation(value = "创建个性化菜单", notes = "tagId: 101 客户, tagId: 100 医生, tagId: null 创建普通菜单")
    @GetMapping("/create/old")
    public Result<?> createMenu(@RequestParam(required = false) Integer tagId) {
        if (WechatConstant.OfficialAccountTag.isValidTag(tagId)) {
            return Result.error("不支持的tagId: " + tagId);
        }
        // 我要加入
        WechatMenuButton menu1 = null;
        if (WechatConstant.OfficialAccountTag.isUserTag(tagId)) {
            menu1 = new WechatMenuButton("进入首页", WechatMenuType.miniprogram.name(), PathUtil.getH5Domain() + "/router.html?page-router=patient");
            menu1.setAppid(WechatUtil.getClientId());
            menu1.setPagepath("/pages/index/index");
        } else if (WechatConstant.OfficialAccountTag.isDoctorTag(tagId)) {
            menu1 = new WechatMenuButton("进入工作室", Constant.WechatMenuType.view.name(), PathUtil.getH5Domain() + "/router.html?page-router=doctor");
        } else {
            WechatMenuButton menu1Sub1 = new WechatMenuButton("我是患者", WechatMenuType.miniprogram.name(), PathUtil.getH5Domain() + "/router.html?page-router=patient");
            menu1Sub1.setAppid(WechatUtil.getClientId());
            menu1Sub1.setPagepath("/pages/index/index");
            WechatMenuButton menu1Sub2 = new WechatMenuButton("我是医生", Constant.WechatMenuType.view.name(), PathUtil.getH5Domain() + "/router.html?page-router=doctor");
            menu1 = new WechatMenuButton("我要加入");
            menu1.setSub_button(menu1Sub1, menu1Sub2);
        }
        log.info("WechatMenuController.createMenu.[tagId] menu1=" + menu1);
        // 科普文章
        WechatMenuButton menu2 = new WechatMenuButton("科普文章");
        WechatMenuButton btn2Sub1 = new WechatMenuButton("饮食健康", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=1");
        WechatMenuButton btn2Sub2 = new WechatMenuButton("运动休闲", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=3");
        WechatMenuButton btn2Sub3 = new WechatMenuButton("血糖监测", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=12");
        WechatMenuButton btn2Sub4 = new WechatMenuButton("药物治疗", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=30");
        WechatMenuButton btn2Sub5 = new WechatMenuButton("并发症", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=31");
        menu2.setSub_button(btn2Sub1, btn2Sub2, btn2Sub3, btn2Sub4, btn2Sub5);
        WechatMenu wechatMenu = new WechatMenu(tagId, menu1, menu2);
        // 创建个性化菜单
        if (tagId != null) {
            wechatApi.createIndividuationMenu(wechatMenu);
        } else {
            // 创建普通菜单
            wechatApi.createCommonMenu(wechatMenu);
        }
        return Result.OK();
    }

    @ApiOperation(value = "创建新版个性化菜单", notes = "tagId: 101 客户, tagId: 100 医生, tagId: null 创建普通菜单")
    @GetMapping("/create")
    public Result<?> createNewMenu(@RequestParam(required = false) Integer tagId) {
        WechatMenuButton menu1 = null, menu2 = null;
        if (!WechatConstant.OfficialAccountTag.isValidTag(tagId)) {
            // 创建默认菜单
            WechatMenuButton menu1Sub1 = new WechatMenuButton("我是患者", WechatMenuType.miniprogram.name(), PathUtil.getH5Domain() + "/router.html?page-router=patient");
            menu1Sub1.setAppid(WechatUtil.getClientId());
            menu1Sub1.setPagepath("/pages/index/index");
            WechatMenuButton menu1Sub2 = new WechatMenuButton("我是医生", Constant.WechatMenuType.view.name(), PathUtil.getH5Domain() + "/router.html?page-router=doctor");
            menu1 = new WechatMenuButton("我要加入");
            menu1.setSub_button(menu1Sub1, menu1Sub2);

            // 科普文章
            menu2 = new WechatMenuButton("科普文章");
            WechatMenuButton btn2Sub1 = new WechatMenuButton("饮食健康", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=1");
            WechatMenuButton btn2Sub2 = new WechatMenuButton("运动休闲", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=3");
            WechatMenuButton btn2Sub3 = new WechatMenuButton("血糖监测", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=12");
            WechatMenuButton btn2Sub4 = new WechatMenuButton("药物治疗", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=30");
            WechatMenuButton btn2Sub5 = new WechatMenuButton("并发症", Constant.WechatMenuType.view.name(), "http://h5.lingduhealth.com/dtrd-website/knowledge.html?articleCategory=31");
            menu2.setSub_button(btn2Sub1, btn2Sub2, btn2Sub3, btn2Sub4, btn2Sub5);
        } else {
            if (WechatConstant.OfficialAccountTag.isUserTag(tagId)) {
                // 创建患者菜单
                menu1 = new WechatMenuButton("进入首页", WechatMenuType.miniprogram.name(), PathUtil.getH5Domain() + "/router.html?page-router=patient");
                menu1.setAppid(WechatUtil.getClientId());
                menu1.setPagepath("/pages/index/index");
                menu2 = new WechatMenuButton("常用功能", WechatMenuType.view.name(), PathUtil.getH5Domain() + "/router.html?page-router=patient_menu");
            } else if (WechatConstant.OfficialAccountTag.isDoctorTag(tagId)) {
                // 创建医生菜单
                menu1 = new WechatMenuButton("进入工作室", Constant.WechatMenuType.view.name(), PathUtil.getH5Domain() + "/router.html?page-router=doctor");
                menu2 = new WechatMenuButton("常用功能", WechatMenuType.view.name(), PathUtil.getH5Domain() + "/router.html?page-router=doctor_menu");
            } else if (WechatConstant.OfficialAccountTag.isConsultantTag(tagId)) {
                // 创建顾问菜单
                menu1 = new WechatMenuButton("常用功能", WechatMenuType.view.name(), PathUtil.getH5Domain() + "/router.html?page-router=doctor_menu");
            }
        }
        log.info("WechatMenuController.createMenu.[tagId] menu1=" + menu1);
        WechatMenu wechatMenu = new WechatMenu(tagId, menu1, menu2);
        // 创建个性化菜单
        if (tagId != null) {
            wechatApi.createIndividuationMenu(wechatMenu);
        } else {
            // 创建普通菜单
            wechatApi.createCommonMenu(wechatMenu);
        }
        return Result.OK();
    }


    @ApiOperation("删除菜单")
    @GetMapping("/remove")
    public Result<?> removeMenu() {
        WechatResponse response = wechatApi.removeMenu();
        return Result.OK(response);
    }

    @ApiOperation("查询菜单")
    @GetMapping("/query")
    public Result<?> getApi(@RequestParam(required = false) String appId, @RequestParam(required = false) String appSecret) {
        JSONObject object = wechatApi.queryOfficeAccountMenu(appId, appSecret);
        return Result.OK(object);
    }

}
